package com.dao.impl;

import com.dao.BaseDao;
import com.dao.CommentDao;
import com.entity.Comment;
import com.entity.CommentExample;
import com.mapper.CommentMapper;
import com.page.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;

@Repository
public class CommentDaoImp extends BaseDao implements CommentDao {

	@Resource
    CommentMapper commentMapper;
	@Resource
	protected RedisTemplate<Serializable,Serializable> redisTemplate;

	/**
	 *
	 * @param pid 父id 常量的类别id
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@Cacheable(value = "myCache",key = "#cacheId")
	public List<Comment> getCommentByPid(int pid,String cacheId) throws SQLException, ClassNotFoundException {
		CommentExample commentExample = new CommentExample();
		commentExample.createCriteria().andCopidEqualTo(pid);
		return commentMapper.selectByExample(commentExample);
	}
	

	/**
	 * 增加一个常量
	 * 
	 * @param comment
	 *            常量对象
	 * @return 返回插入的状态值 数字 为1成功 0失败 -值异常
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws SQLException
	 *             抛出sql异常
	 */
	public int addComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException {
		String keyStr = "commentByPid"+comment.getCopid();
		System.out.println("commentByPid"+comment.getCopid());
//	    Serializable serializable = new GenericJackson2JsonRedisSerializer().serialize(keyStr);
//		System.out.println(serializable);
		redisTemplate.delete(keyStr);
		return commentMapper.insertSelective(comment);
	}

	/**
	 * 修改常量信息
	 * @param comment
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int updateComment(Comment comment) throws IllegalArgumentException, IllegalAccessException, SQLException {
		return commentMapper.updateByPrimaryKeySelective(comment);
	}

	/**
	 * 根据主键删除
	 * @param coId
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public int deleteComment(int coId) throws IllegalArgumentException, IllegalAccessException, SQLException {
		return commentMapper.deleteByPrimaryKey(coId);
	}

	/**
	 * 根据常量分类 分页查询，分页查询 就是 mysql的limit 语句需要2个参数 参数1从几开始取 参数2 取多长
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<Comment> queryCommentsByPage(PageInfo pageInfo, int pid) throws Exception {
		int currPage = pageInfo.getCurrPage();
		int pageSize = pageInfo.getPageSize();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("currPage", currPage);
		data.put("pageSize", pageSize);
		data.put("copid",pid);//将多传的参数 放入参数 map 在xml文件中 访问map对应的变量获取
		System.out.println("---------------"+pageSize+"----------------------");
		CommentExample commentExample =  new CommentExample();
		commentExample.createCriteria().andCopidEqualTo(pid);
		pageInfo.setItemCount((int) commentMapper.countByExample(commentExample));
		return commentMapper.queryCommentsByPage(data);
	}
}
